package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

	@Autowired
	private DishMapper dishMapper;
	@Autowired
	private DishFlavorMapper dishFlavorMapper;
	@Autowired
	private SetmealDishMapper setmealIdMapper;



	/**
	 * 新增菜品
	 * @param dishDTO
	 */
	@Override
	@Transactional
	public void save(DishDTO dishDTO) {
		//向菜品表插入数据
		Dish dish = new Dish();
		BeanUtils.copyProperties(dishDTO, dish);
		dishMapper.insert(dish);
		//获取insert生成的主键值
		Long dishId = dish.getId();

		List<DishFlavor> flavors=dishDTO.getFlavors();
		//判断口味集合是否为空
		if(!flavors.isEmpty()&&flavors!=null){
			//给dishId赋值
			flavors.forEach(dishFlavor -> {
				dishFlavor.setDishId(dishId);
			});
			//向口味表插入n条数据
			dishFlavorMapper.insert(flavors);
		}
	}


	/**
	 * 菜品分页查询
	 * @param dishPageQueryDTO
	 * @return
	 */
	@Override
	public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
		PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
		Page<DishVO> page =dishMapper.pageQuery(dishPageQueryDTO);
		return new PageResult(page.getTotal(),page.getResult());
	}


	/**
	 * 批量删除菜品
	 * @param ids
	 */
	@Override
	@Transactional
	public void delete(List<Long> ids) {
		//判断是否在起售状态
		for(Long id:ids){
			Dish dish = dishMapper.getByid(id);
			if(dish.getStatus()== StatusConstant.ENABLE){
				throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);

			}
		}
		//判断查看是否关联套餐
		for(Long id:ids){
			List<Long> setmeslIds = setmealIdMapper.getsetmealIdByDishIds(ids);
			if(setmeslIds!=null&&setmeslIds.size()>0){
				throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
			}
		}
			//批量删除菜品
			dishMapper.deleteById(ids);
			//删除菜品风味
			dishFlavorMapper.deleteByDishIds(ids);
	}

	/**
	 * 根据主键id获取菜品
	 * @param id
	 * @return
	 */
	@Override
	public DishVO getById(Long id) {
		//获取菜品
		Dish dish = dishMapper.getByid(id);
		//获取菜品的所有口味
		List<DishFlavor> list =	dishFlavorMapper.getByDishId(id);
		//将查询得到的数据封装到dishvo
		DishVO dishVO = new DishVO();
		BeanUtils.copyProperties(dish,dishVO);
		dishVO.setFlavors(list);
		return dishVO;

	}

	/**
	 * 根据id修改菜品信息和口味信息
	 * @param dishDTO
	 */
	@Override
	@Transactional
	public void update(DishDTO dishDTO) {
		Dish dish = new Dish();
		BeanUtils.copyProperties(dishDTO, dish);
		//修改菜品
		dishMapper.update(dish);


		//先删除原来的口味，再添加口味。实现口味更新
		//封装菜品id
		List<Long> ids=new ArrayList<>();
		ids.add(dish.getId());
		//删除口味
		dishFlavorMapper.deleteByDishIds(ids);

		//获得口味集合
		List<DishFlavor> flavors=dishDTO.getFlavors();
		//判断口味集合是否为空
		if(!flavors.isEmpty()&&flavors!=null){
			//给dishId赋值
			flavors.forEach(dishFlavor -> {
				dishFlavor.setDishId(dish.getId());
			});
			//向口味表插入n条数据
			dishFlavorMapper.insert(flavors);
		}
	}

	/**
	 * 根据分类id获取菜品
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<Dish> getByCategoryId(Long categoryId) {
		Dish dish = Dish.builder()
				.categoryId(categoryId)
				.status(StatusConstant.ENABLE)
				.build();
		List<Dish> dishs =dishMapper.list(dish);
		return dishs;
	}

	/**
	 * 根据id起售和停售菜品
	 * @param status
	 * @param id
	 */
	@Override
	public void startOrStop(Integer status, Long id) {
		dishMapper.updateStatus(status,id);
	}


	/**
	 * 条件查询菜品和口味
	 * @param dish
	 * @return
	 */
	public List<DishVO> listWithFlavor(Dish dish) {
		List<Dish> dishList = dishMapper.list(dish);

		List<DishVO> dishVOList = new ArrayList<>();

		for (Dish d : dishList) {
			DishVO dishVO = new DishVO();
			BeanUtils.copyProperties(d,dishVO);

			//根据菜品id查询对应的口味
			List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

			dishVO.setFlavors(flavors);
			dishVOList.add(dishVO);
		}
		return dishVOList;
	}
}
